/**
 * @作者 faner
 * @创建时间 2021/1/8 16:11
 */
package com.faner.fast.excel.converters;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.faner.fast.constant.enums.GenderEnum;

public class GenderConverter implements Converter<Integer> {


    @Override
    public Class supportJavaTypeKey() {
        return GenderEnum.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 读取时用
     * @param cellData
     * @param excelContentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (GenderEnum.MALE.getDescription().equals(cellData.getDataFormatString())) {
            return 1;
        }else if(GenderEnum.FEMALE.getDescription().equals(cellData.getDataFormatString())){
            return 2;
        }
        return 0;
    }

    /**
     * 导出到excel
     * @param code 行值
     * @param excelContentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public CellData convertToExcelData(Integer code, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if(GenderEnum.MALE.getCode().equals(code)){
            return new CellData(GenderEnum.MALE.getDescription());
        }else if(GenderEnum.FEMALE.getCode().equals(code)){
            return new CellData(GenderEnum.FEMALE.getDescription());
        }
        return new CellData(GenderEnum.UNKNOWN.getDescription());
    }
}
